#include "StdAfx.h"
#include "BackBuffer.h"

void BackBuffer::Cleanup(){
	if(BackBufferDC){ ::DeleteDC(BackBufferDC); BackBufferDC = 0;}
	if(EmptyDC){ ::DeleteDC(EmptyDC); EmptyDC = 0;}
//	if(BBB){ DeleteObject(BBB); BBB = 0;}
}


void BackBuffer::ReCreateBackBuffer(HWND hWnd_i){
	Cleanup();
	hWnd = hWnd_i;
	HDC hdc = ::GetDC(hWnd_i);
	BackBufferDC = ::CreateCompatibleDC ( hdc );
	EmptyDC = ::CreateCompatibleDC ( hdc );
	ResetBBDC(BackBufferDC);
}

void BackBuffer::DeleteBBDC(HDC& hdc_io){
	if(hdc_io){
		::DeleteDC(hdc_io); 
		hdc_io = 0;
	}
}

HDC BackBuffer::CreateBBDC(){
	_ASSERT(hWnd);
	HDC hdc = ::GetDC(hWnd);
	return ::CreateCompatibleDC ( hdc );
}

void BackBuffer::ResetMemDC(){
	ResetBBDC(BackBufferDC);
}

void BackBuffer::ResetBBDC(HDC hdc_i){
	_ASSERT(hdc_i);
	_ASSERT(hWnd);
	HDC hdc = ::GetDC(hWnd);
	HBITMAP BBB= ::CreateCompatibleBitmap (hdc, Width, Height);
	::ReleaseDC(hWnd, hdc);
	::SelectObject (hdc_i, BBB );
	::DeleteObject(BBB);
}

BackBuffer::BackBuffer(){
	BackBufferDC = 0;
	EmptyDC = 0;
	Width = 0;
	Height = 0;
	hWnd = 0;
}